setwd('C:/Users/test/Desktop/Lucas/Ciência Política UFPE - Mestrado/Publicações e eventos/2019/lapop/paper')

# packages
library(tidyverse)
library(broom)
library(foreign)
library(readstata13)
library(data.table)
library(naniar)
library(plyr)
library(psych)


##### LOADING DATA #####
# lapop grand merge
grandmerge <- read.dta13('../ab_04-14.dta')

# country individual data
setwd('./original data/2016 wave')

ar <- read.dta('argentina.dta')
be <- read.dta13('belize.dta')
bo <- read.dta('bolivia.dta')
br <- read.dta('brazil.dta')
ch <- read.dta('chile.dta')
co <- read.dta('colombia.dta')
cr <- read.dta('costa rica.dta')
do <- read.dta('dominica.dta')
ec <- read.dta('ecuador.dta')
es <- read.dta('el salvador.dta')
gu <- read.dta('guatemala.dta')
ha <- read.dta('haiti.dta')
ho <- read.dta('honduras.dta')
ja <- read.dta('jamaica.dta')
me <- read.dta('mexico.dta')
ni <- read.dta('nicaragua.dta')
pa <- read.dta('panama.dta')
par <- read.dta('paraguay.dta')
pe <- read.dta('peru.dta')
ur <- read.dta('uruguay.dta')
ve <- read.dta('venezuela.dta')

##### BINDING DATASETS #####
# country datasets - 2016 wave
merge16 <- rbindlist(list(ar, be, bo, br, ch, co, cr, do, ec, es, gu, ha, ho, ja,
                          me, ni, pa, par, pe, ur, ve), fill = T)

# merging with grand merge
g_merge <- rbindlist(list(grandmerge, merge16), fill = T)

# selecting interest variables for the dataset
bd <- g_merge %>% dplyr::select(pais, wave, idnum, prov, municipio, municipio04, municipio06, 
                         municipio08, municipio10, cluster, ur, tamano, fecha, wt, 
                         weight1500, q1, q2, a4, a4_04, a4_06, a4c, abs5, aoj12,
                         argbv4, argexp1, arm2, aut1, b1, b2, b3, b4, b6, b10a, 
                         b11, b12, b13, b14, b15, b16, b17, b18, b19, b20, b20a, 
                         b21, b21a, b21e, b23, b24, b31, b31a, b32, b33, b37, 
                         b43, b46, b47, b47a, bahexc11, boldi1, boldi2, exp1b, 
                         dc1, dc10, dc13, dem2, dem6, dem11, dem13, dem13a, dem13b,
                         dem13c, dem23, exc7, exc7mil, exc11, jc13, jc12, jc13a, 
                         jc15, jc15a, jc16, jc16a, l1, l1b, muni2a, n9, vivcorr1, 
                         vivcorr2, camp1, pn4, pol1, pol2, n15, pop101, pop102, 
                         vb2, vb3n_14, vb4new, vb20, soct1, soct2, soct3, jc1,
                         jc1rr, jc4, jc13, jc10, ing4)

# removing objects
rm(ar, be, bo, br, ch, co, cr, do, ec, es, grandmerge, gu, ha, ho, ja,
   me, merge16, ni, pa, par, pe, ur, ve)

##### RELOADING DATASET #####
# bd <- read.csv('../analysis data/ab_04-16.csv')

##### ADJUSTING VARIABLES #####
# handling with na responses
bd$wave[is.na(bd$wave)] <- 2016

# urban or rural
bd$ur[bd$ur == '.z'] <- NA

# size of location
bd$tamano[bd$tamano == '.z'] <- NA

# gender
bd$q1[bd$q1 == '.a'] <- NA

# age
bd$q2[bd$q2 %in% c('.a', '.b', '.c', '.z')] <- NA

# most important problem
bd$a4[bd$a4 %in% c('.a', '.b', '.c', '.z')] <- NA

# 1st problem of country
bd$a4_04[bd$a4_04 %in% c('.a', '.c', '.z')] <- NA

# most serious problem
bd$a4_06[bd$a4_06 %in% c('.a', '.c', '.z')] <- NA

# satisfaction with democracy
bd$pn4[bd$pn4 %in% c('.a', '.b', '.c')] <- NA

###### RE-CODING OBSERVATIONS #####

# satisfaction with democracy

# creating new variable as numeric
bd$stf_dem <- bd$pn4 %>% as.numeric()
table(bd$stf_dem)

# renaming observations
bd$stf_dem[bd$stf_dem == 8] <- 1
bd$stf_dem[bd$stf_dem == 9] <- 2
bd$stf_dem[bd$stf_dem == 10] <- 3
bd$stf_dem[bd$stf_dem == 11] <- 4

# re-leveling as factor
bd$stf_dem <- bd$stf_dem %>% as.factor()
levels(bd$stf_dem) <- c('Very Satisfied', 'Satisfied', 'Dissatisfied', 
                        'Very Dissatisfied')

# creating numeric variable
bd$stf_dem_num <- bd$stf_dem %>% as.numeric()


# inverting variable
bd$dstf_dem <- bd$stf_dem_num
table(bd$dstf_dem)

# creating dummy variable for very dissatisfied
bd$very_dis <- ifelse(bd$stf_dem_num == 4, 1, 0)

# corruption perception

# creating new variable as numeric
summary(exc7mil)
bd$cor_p <- bd$exc7 %>% as.numeric()
table(bd$cor_p)

# replacing values
bd$cor_p[bd$cor_p == 9] <- 1
bd$cor_p[bd$cor_p == 10] <- 2
bd$cor_p[bd$cor_p == 11] <- 3
bd$cor_p[bd$cor_p == 12] <- 4
bd$cor_p[bd$cor_p == 15] <- 1
bd$cor_p[bd$cor_p == 16] <- 2
bd$cor_p[bd$cor_p == 17] <- 3
bd$cor_p[bd$cor_p == 18] <- 4

# creating factor
bd$cor_p <- bd$cor_p %>% as.factor()
levels(bd$cor_p) <- c('Very widespread', 'Somewhat widespread', 'Not very widespread', 
                      'Not widespread at all')

# creating numeric variable
bd$cor_p_num <- bd$cor_p %>% as.numeric()

# inverting variable
bd$cor_p_num <- max(bd$cor_p_num, na.rm = T) - bd$cor_p_num
table(bd$cor_p_num)

# gender
table(bd$q1)
str(bd$q1)

# creating numeric variable
bd$gender <- bd$q1 %>% as.numeric()

# replacing values
bd$gender[bd$gender %in% c(6, 7)] <- c(1, 2)

# creating dummy variable for males
bd$gender <- ifelse(bd$gender == 1, 1, 0)

# perception of economy
table(bd$n15)
str(bd$n15)

table(bd$soct1)

# creating numeric variable for perception of national economy situation
bd$bad_ecnm <- bd$soct1 %>% as.numeric()
table(bd$bad_ecnm)

# retrospective evaluation of economy
table(bd$soct2)

# converting for numeric
bd$ecnm_retro <- bd$soct2 %>% as.numeric()
        
# replacing values
bd$ecnm_retro[bd$ecnm_retro == 8] <- 1
bd$ecnm_retro[bd$ecnm_retro == 9] <- 2
bd$ecnm_retro[bd$ecnm_retro == 10] <- 3

# economy future
bd$ecnm_pros <- bd$soct3 %>% as.numeric()

# creating variable for people who believes corruption as the main problem of your country
table(bd$a4)

bd$corr_mip <- ifelse(bd$a4 %in% c('Corruption', 'Corrupción'), 1, 0)
table(bd$corr_mip)

# military coup justified
table(bd$jc1rr)

bd$coup_sup <- ifelse(bd$jc1rr == 100, 1, 0)

# coup justified under some circumstances
# when crime is high
table(bd$jc10)

# creating numeric variable
bd$coup_crime <- bd$jc10 %>% as.numeric()
table(bd$coup_crime)

# replacing values
bd$coup_crime[bd$coup_crime == 7] <- 1
bd$coup_crime[bd$coup_crime == 8] <- 2
bd$coup_crime[bd$coup_crime == 2] <- 0

# when corruption is high
table(bd$jc13)

# creating numeric variable
bd$coup_corr <- bd$jc13 %>% as.numeric()
table(bd$coup_corr)

# replacing values
bd$coup_corr[bd$coup_corr == 7] <- 1
bd$coup_corr[bd$coup_corr == 8] <- 2
bd$coup_corr[bd$coup_corr == 2] <- 0

# executive governing without legislature
table(bd$jc15a)

# creating numeric variable
bd$no_legis <- bd$jc15a %>% as.numeric()
table(bd$no_legis)

# replacing values
bd$no_legis[bd$no_legis == 7] <- 1
bd$no_legis[bd$no_legis == 8] <- 2
bd$no_legis[bd$no_legis == 2] <- 0

table(bd$jc16a)

# executive able to dissolve supreme court
bd$exec_diss_supcourt <- bd$jc16a %>% as.numeric()
table(bd$exec_diss_supcourt)

# replacing values
bd$exec_diss_supcourt[bd$exec_diss_supcourt == 2] <- 0

# proud of political system
table(bd$b4)
str(bd$b4)

# support the political system
table(bd$b6)

# trust in electoral institution
table(bd$b11)

# trust in national legislature
table(bd$b13)

# trust in government
table(bd$b14)

# trust in elections
table(bd$b47a)

bd$trst_elec <- bd$b47 %>% as.numeric()

table(bd$trst_elec)

bd$trst_elec2 <- bd$b47a %>% as.numeric()
table(bd$trst_elec2)

##### EXPORTING DATASET #####
# creating directory
# dir.create('../../analysis data')

# removing non LAC-21 countries
bd <- bd %>% filter(!pais %in% c('Suriname', 'Guyana', 'Canada', 'United States',
                                 'Trinidad and Tobago', 'Bahamas', 'Barbados'))

count(bd$pais)

# recoding countries
bd$pais[bd$pais == 'Haití'] <- 'Haiti'
bd$pais[bd$pais == 'Belice'] <- 'Belize'

# writing dataset
write.csv(bd, './analysis data/ab_04-16.csv')
